package edu.hunan.cxb18.service.impl;

import edu.hunan.cxb18.error.impl.CommonErrorImpl;
import edu.hunan.cxb18.error.impl.UserException;
import edu.hunan.cxb18.mapper.UserMapper;
import edu.hunan.cxb18.pojo.User;
import edu.hunan.cxb18.pojo.UserExample;
import edu.hunan.cxb18.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.sql.Time;
import java.util.List;

/**
 * @author AuthorName
 * @create 2020-04-22 15:07
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(User user) throws UserException{
        User user1 = userMapper.selectByUsername(user.getUsername());
        if (user1 == null){
            throw new UserException(CommonErrorImpl.PARAMETER_VALIDATION_ERROR,"用户名不存在");
        }
        if (!StringUtils.pathEquals(user1.getPwd(),user.getPwd())){
            throw new UserException(CommonErrorImpl.PARAMETER_VALIDATION_ERROR,"密码错误");
        }
        return user1;
    }

    @Override
    public void register(User user) throws UserException {
        if (user == null){
            throw new UserException(CommonErrorImpl.PARAMETER_VALIDATION_ERROR);
        }
        if (StringUtils.isEmpty(user.getUsername())||StringUtils.isEmpty(user.getPwd())){
            throw new UserException(CommonErrorImpl.PARAMETER_VALIDATION_ERROR);
        }
        try {
            userMapper.insertSelective(user);
        }catch (RuntimeException e){
            throw new UserException(CommonErrorImpl.PARAMETER_VALIDATION_ERROR,"用户名已经存在");
        }
    }
}
